#-*- coding: utf-8 -*-

import wx
from functions import Brighter
import imagecheckbox

class CalendarList(wx.VListBox):
    calendars = []
    def __init__(self, parent):
        wx.VListBox.__init__(self, parent);

    def OnMeasureItem(self, n):
        return 24
    
    def OnDrawItem(self, dc, rect, n):

        font = dc.GetFont()
        font.SetWeight(wx.FONTWEIGHT_NORMAL)
        font.SetUnderlined(False)
        dc.SetFont(font)
        
        pen = wx.Pen(wx.Color(255,255,255), 3)
        dc.SetPen(pen)
        bg = wx.Color()
        bg.SetFromName(self.calendars[n][1][0].color.value)
        
        bg = Brighter(bg)
        fg = wx.Color(255,255,255)
        dc.SetTextForeground(fg)
        dc.SetBrush(wx.Brush(bg))
        dc.DrawRectangleRect(rect)
        
        if self.calendars[n][2]:
            dc.DrawBitmap(imagecheckbox.getCheckedBitmap(), rect.x+4, rect.y+4, True)
        else:
            dc.DrawBitmap(imagecheckbox.getUncheckedBitmap(), rect.x+4, rect.y+4, True)
            
        rect.x += 24
        dc.DrawLabel(self.calendars[n][1][0].title.text, rect, 
                     wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
        
#        self.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
        self.Bind(wx.EVT_LEFT_DCLICK, self.OnDClick)
        
    def Reset(self):
        self.SetItemCount(len(self.calendars))
        self.Refresh()

    def Toggle(self, n):
        self.calendars[n][2] = not self.calendars[n][0][2]

#    def OnRightClick(self, event):
#        n = self.GetItemFromY(event.GetY())
#        title = self.calendars[n][1].title.text
#        menu = wx.Menu()
#        menu.Append(-1, title)
#        self.PopupMenuXY(menu, event.GetX(), event.GetY())

    def OnDClick(self, event):
#        self.Toggle(event.GetInt())
        n = self.GetItemFromY(event.GetY())
        if n != -1:
            wx.MessageBox(self.calendars[n][1][0].title.text)
        
    def GetItemFromY(self, y):
        n = y/24
        if n > self.GetItemCount() - 1: n = -1
        return n
        